package com.zuo.demo01.controller.blogs;


import com.zuo.demo01.pojo.Blog;
import com.zuo.demo01.pojo.Tag;
import com.zuo.demo01.service.BlogService;
import com.zuo.demo01.service.ConfigService;
import com.zuo.demo01.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MainController {

    @Autowired
    private BlogService blogService;


    @Autowired
    private ConfigService configService;

    @Autowired
    private TagService tagService;


    @RequestMapping("/blog/search")
    public String search(@RequestParam("key") String key,Model model){
        return search_page(1,10,key,model);
    }

    @RequestMapping("/blog/search/page")
    public String search_page(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,@RequestParam("key") String key, Model model){
        Map<String,Object> map =new HashMap<>();
        map.put("startIndex",(page-1)*limit);
        map.put("pageSize",limit);
        map.put("key",key);
        List<Blog> blogs = blogService.findByKey(map);
        Map<String, String> configs = configService.findAll();

        List<Tag> tags = tagService.findAll();
        model.addAttribute("tags",tags);

        List<Blog> newBlogs = blogService.findByType(0, 10);
        model.addAttribute("newBlogs",newBlogs);

        List<Blog> hotBlogs = blogService.findByType(1, 10);
        model.addAttribute("hotBlogs",hotBlogs);

        model.addAttribute("blogs",blogs);
        model.addAttribute("configs",configs);
        model.addAttribute("count",blogService.countByKey(key));
        model.addAttribute("curr",page);
        model.addAttribute("key",key);
        return "blogs/index";
    }


    @RequestMapping("/blog/category/{category_name}")
    public String blogs_by_category(@PathVariable("category_name") String category_name,Model model){
        return category_page(1,10,category_name,model);
    }


    @RequestMapping("/blog/category/page")
    public String category_page(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit
            ,@RequestParam("category_name") String category_name, Model model){
        Map<String,Object> map =new HashMap<>();
        map.put("startIndex",(page-1)*limit);
        map.put("pageSize",limit);
        map.put("categoryName",category_name);

        List<Blog> blogs = blogService.findByCategory(map);
        Map<String, String> configs = configService.findAll();

        List<Tag> tags = tagService.findAll();
        model.addAttribute("tags",tags);

        List<Blog> newBlogs = blogService.findByType(0, 10);
        model.addAttribute("newBlogs",newBlogs);

        List<Blog> hotBlogs = blogService.findByType(1, 10);
        model.addAttribute("hotBlogs",hotBlogs);

        model.addAttribute("blogs",blogs);
        model.addAttribute("configs",configs);
        model.addAttribute("count",blogService.countByCategory(category_name));
        model.addAttribute("curr",page);
        model.addAttribute("category_name",category_name);
        return "blogs/index";
    }


    @RequestMapping("/blog/tag/{tag_name}")
    public String blogs_by_tag(@PathVariable("tag_name") String tag_name,Model model){
        return tag_page(1,10,tag_name,model);
    }


    @RequestMapping("/blog/tag/page")
    public String tag_page(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit
            ,@RequestParam("tag_name") String tag_name, Model model){
        Map<String,Object> map =new HashMap<>();
        map.put("startIndex",(page-1)*limit);
        map.put("pageSize",limit);
        map.put("tagName",tag_name);

        List<Blog> blogs = blogService.findByTag(map);

        Map<String, String> configs = configService.findAll();

        List<Tag> tags = tagService.findAll();
        model.addAttribute("tags",tags);

        List<Blog> newBlogs = blogService.findByType(0, 10);
        model.addAttribute("newBlogs",newBlogs);

        List<Blog> hotBlogs = blogService.findByType(1, 10);
        model.addAttribute("hotBlogs",hotBlogs);

        model.addAttribute("blogs",blogs);
        model.addAttribute("configs",configs);
        model.addAttribute("count",10);
        model.addAttribute("curr",page);
        model.addAttribute("tag_name",tag_name);
        return "blogs/index";
    }







}
